package com.sbp.message.common;   
  
import java.io.BufferedInputStream;   
import java.io.FileInputStream;   
import java.io.FileNotFoundException;   
import java.io.FileOutputStream;   
import java.io.IOException;   
import java.io.InputStream;   
import java.io.OutputStream;   
import java.util.Properties;

import com.dap.exception.PlatformException;
import com.sbp.message.exception.ExceptionConstants;   
  
  
/**  
* @author  
* @version  
*/   
public abstract class OperatePropertiesUtils {
  
    /**  
    * 根据主键key读取主键的值value  
    * @param filePath 属性文件路径  
    * @param key 键名  
    */   
    public static String readValue(String filePath, String key) {   
    	
        Properties props = new Properties();   
        try {   
            InputStream in = new BufferedInputStream(new FileInputStream(   
                    filePath));   
            props.load(in);   
            String value = props.getProperty(key);   
            System.out.println(key +"键的值是："+ value);   
            return value;   
        } catch (Exception e) {   
            throw new PlatformException(ExceptionConstants.FILE_READ_EXCEPTION);
        }   
    }   
      
    /**  
    * 更新（或插入）一对properties信息(主键及其键值)  
    * 如果该主键已经存在，更新该主键的值；  
    * 如果该主键不存在，则插件一对键值。  
    * @param keyname 键名  
    * @param keyvalue 键值  
    */   
    public static void writeProperties(String path,String keyname,String keyvalue) {          
        try {   
        	// 准备配置信息
            Properties properties = new Properties();
            properties.setProperty(keyname, keyvalue);
            // 准备
            OutputStream out = new FileOutputStream(path);
            String comments = "这是我的配置文件";
            // 写出去
            properties.store(out, comments);
            out.close(); 
        } catch (IOException e) {   
        	throw new PlatformException(ExceptionConstants.FILE_WRITE_EXCEPTION);   
        }      
    }   
  
} 